<template>
  <div class="home_box">
    <!-- 搜索框 -->
    <div class="header_box">
      <div class="title">
        <p>网易严选</p>
      </div>

      <div class="search">
        <van-search class="search-box" placeholder="搜索商品,共185075款好物" @click="toSearch" />
        <div class="btn">
          <van-button plain type="danger" size="small" v-if="!token" @click="toLogin"
            ><span>登录</span></van-button
          >
        </div>
      </div>
    </div>
    <div class="change_box">
      <!-- 切换页面 -->
      <van-tabs v-model:active="active" @click-tab="clickTab">
        <van-tab v-for="item in tabList" :title="item.name" :key="item.id">
          <component :is="tab ? toHome : toOther" :id="item.id" />
        </van-tab>
      </van-tabs>
      <div class="cart">
        <van-icon name="arrow-down" @click="show = true" />
        <van-overlay :show="show" @click="show = false">
          <div class="wrapper" @click.stop>
            <div class="block" @click="show = false">
              <div class="header_box">
                <div class="title">
                  <p>网易严选</p>
                </div>

                <div class="search">
                  <van-search
                    class="search-box"
                    placeholder="搜索商品,共185075款好物"
                    @click="toSearch"
                  />
                  <div class="btn">
                    <van-button plain type="danger" size="small" v-if="!token" @click="toLogin"
                      ><span>登录</span></van-button
                    >
                  </div>
                </div>
              </div>
              <div class="title">
                <p>全部频道</p>
                <van-icon name="arrow-up" @click="show = false" />
              </div>

              <div class="tab">
                <div v-for="item in tabList" :key="item.id">
                  <span @click="changContent(item.id)">{{ item.name }}</span>
                </div>
              </div>
            </div>
          </div>
        </van-overlay>
      </div>
      <!-- 下拉切换页面 -->
    </div>
    <van-back-top right="5vw" bottom="15vh" offset="800" />
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import toHome from '@/pages/Home/component/ToHome/index.vue'
import toOther from '@/pages/Home/component/ToOther/index.vue'
import { useRouter } from 'vue-router'
import { reqHomeCateList } from '@/api/home'
import { IReqCateListItem } from '@/types/home'
import { onMounted } from 'vue'
import { useUserStore } from '@/store/user'
import { storeToRefs } from 'pinia'
const { token } = storeToRefs(useUserStore())

const active = ref(0)

const tab = ref(true)

const show = ref(false)

const name = ref<string>('')

// const tabItem = ref<string>('推荐')

const router = useRouter()
const toSearch = () => {
  router.push('/search')
}
const toLogin = () => {
  router.push('/login')
}

const tabList = ref<IReqCateListItem[]>([])

const getHomeCateList = async () => {
  const res = await reqHomeCateList()
  res.data.data.categoryList.unshift({ id: 0, name: '推荐' })
  tabList.value = res.data.data.categoryList
  name.value = res.data.data.currentCategory.name
}

const clickTab = async ({ name }: any) => {
  name === 0 ? (tab.value = true) : (tab.value = false)
}

const changContent = (id: number) => {
   
  id === 0 ? tab.value =true : tab.value = false
}

onMounted(() => {
  getHomeCateList()
})
</script>
<script lang="ts">
export default {
  name: 'HomeView'
}
</script>

<style scoped lang="less">
.home_box {
  width: 375px;
}
.van-back-top{
  width: 100px;
}
.header_box {
  display: flex;
  justify-content: space-evenly;

  .title {
    width: 150px;
    p {
      font-size: 20px;
      font-weight: bold;
      padding-left: 15px;
    }
  }

  .search {
    display: flex;
    justify-content: space-between;
    span {
      font-size: 12px;
    }
    :deep(.van-button) {
      width: 50px;
    }
    .btn {
      margin-top: 13px;
    }
  }
}
.change_box {
  display: flex;
  justify-content: space-evenly;
  width: 370px;

  .van-tabs {
    width: 370px;
    height: 40px;
    position: relative;
  }
  .cart {
    width: 15px;
    right: 0;
    position: absolute;
    z-index: 10;
    background-color: #fff;
    height: 40px;
    .van-icon {
      width: 10px;
      height: 40px;
      margin-top: 15px;
      font-size: 15px;
    }
  }
  .top {
    width: 100%;
    height: 30px;
    background-color: skyblue;
    .right {
      width: 10%;
      background-color: yellow;
      float: right;
    }
  }

  .wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
  }

  .block {
    position: fixed;
    top: 0;
    left: 0;
    width: 370px;
    height: 350px;
    background-color: #fff;
    .title {
      display: flex;
      justify-content: space-between;

      p {
        font-size: 16px;
        display: inline-block;
        padding-left: 5px;
      }
      .van-icon {
        padding-right: 5px;
        width: 20px;
      }
    }
    .tab {
      width: 365px;
      margin-left: 10px;
      display: flex;
      justify-content: space-around;
      flex-wrap: wrap;
      span {
        margin: 5px;
        width: 70px;
        height: 20px;
        display: block;
        border: 1px solid black;
        font-size: 14px;
        background-color: rgb(240, 234, 234);
        text-align: center;
        line-height: 20px;
      }
    }
  }
}
</style>
